import Vue from 'vue'
import VueRouter from 'vue-router'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Layout',
    redirect: '/system/welcome',
    component: () => import('../views/Layout/index.vue'),
    children: [
      {
        path: '/system/welcome',
        name: 'Welcome',
        component: () => import('../views/system/welcome/welcome.vue') // 系统欢迎页
      },
      {
        path: '/system/files',
        name: 'files',
        component: () => import('../views/system/files/index.vue') // 系统欢迎页
      },
      {
        path: '/system/users',
        name: 'Users',
        component: () => import('../views/system/user/user.vue') // 用户管理
      },
      {
        path: '/system/departments',
        name: 'Departments',
        component: () => import('../views/system/departments/index.vue') // 部门管理
      },
      {
        path: '/system/roles',
        name: 'Departments',
        component: () => import('../views/system/roles/index.vue') // 角色管理
      },
      {
        path: '/system/icon',
        name: 'icon',
        component: () => import('../views/system/icon/index.vue') // 图标管理
      },
      {
        path: '/system/menus',
        name: 'menu',
        component: () => import('../views/system/menu/index.vue') // 菜单管理
      },
      {
        path: '/monitor/login-log',
        name: 'loginLog',
        component: () => import('../views/monitor/loginLog') // 日志管理
      },
      {
        path: '/monitor/druid',
        name: 'druid',
        component: () => import('../views/monitor/druid') // 日志管理
      },
      {
        path: '/monitor/logs',
        name: 'logs',
        component: () => import('../views/monitor/logs') // 日志管理
      },
      {
        path: '/monitor/swagger-ui',
        name: 'swaggerUi',
        component: () => import('../views/monitor/swaggerUi') // 日志管理
      },
      {
        path: '/covid19/map',
        name: 'map',
        component: () => import('../views/covid19/map') // 全国疫情
      },
      {
        path: '/covid19/health',
        name: 'map',
        component: () => import('../views/covid19/health') // 健康打卡
      },
      {
        path: '/business/product/in-stocks',
        name: 'instocks',
        component: () => import('../views/product/instocks/instocks.vue') // 入库记录
      },
      {
        path: '/business/product/consumers',
        name: 'instocks',
        component: () => import('../views/product/consumers/index.vue') // 物资去处
      },
      {
        path: '/business/product/list',
        name: 'list',
        component: () => import('../views/product/list/index.vue') // 物资资料
      },
      {
        path: '/business/product/categories',
        name: 'categories',
        component: () => import('../views/product/categories/index.vue') // 物资类别
      },
      {
        path: '/business/product/suppliers',
        name: 'suppliers',
        component: () => import('../views/product/suppliers/index.vue') // 物资来源
      },
      {
        path: '/business/product/out-stocks',
        name: 'out-stocks',
        component: () => import('../views/product/outstocks/index.vue') // 发放记录
      },
      {
        path: '/business/product/stocks',
        name: 'stocks',
        component: () => import('../views/product/stocks/index.vue') // 物资库存
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login/index.vue')
  },
  {
    path: '/error/401',
    name: '401',
    component: () => import('../views/error/401.vue')
  },
  {
    path: '*',
    name: '404',
    component: () => import('../views/error/404.vue')
  }
]

const router = new VueRouter({
  routes
})
// 路由前置守卫
// 前置路由守卫  不登录 强制跳转
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.path !== ('/login') && !token) {
    next('/login')
  } else if (to.path === ('/login') && token) {
    // 如果已经登录  不要再去登录页
    next(from.path)
  } else {
    // 如果有了那么放行
    next()
  }
  if (to.path === '/error/401') {
    return next()
  }
  if (token) {
    const urls = JSON.parse(localStorage.getItem('user')).url
    const isAdmin = JSON.parse(localStorage.getItem('user')).isAdmin
    if (isAdmin) {
      next()
    } else {
      if (urls.length === 0) {
        console.log(133)
        next('/error/401')
      } else {
        if (urls.includes(to.path)) {
          console.log(888)
          next()
        } else {
          console.log(999)
          next('/error/401')
        }
      }
    }
  }
})
router.afterEach(() => {
  nprogress.done() // 路由进入后关闭进度条
})
export default router
